<template>
  <a-modal :open="open" width="1100px" title="批量处方" centered destroy-on-close @cancel="onCancel">
    <div class="ml-4 flex-auto min-w-0">
      <BaseTable
        :height="380"
        :data-source="tableData"
        column-code="000428"
        row-key="id"
        :expanded-row-keys="selectedRowKeys"
        :row-selection="{ selectedRowKeys, onChange: onSelectChange }"
      >
        <template #bodyCell="{ column, record }">
          <template v-if="column.dataIndex === 'sfxm'">
            <MedicineInfo :value="{ mc: record.sfxm?.mc, gg: record.sfxm?.gg, cj: record.sfxm?.cj }" />
          </template>
          <template v-if="column.dataIndex === 'yf'">
            {{ record.yf?.fyff }}
          </template>
          <template v-if="column.dataIndex === 'jlsl'"> {{ record.jlsl || 0 }} {{ record.sfxm?.jldw }} </template>
          <template v-if="column.dataIndex === 'cfts'"> {{ record.cfts }}天 </template>
          <template v-if="column.dataIndex === 'sl'"> {{ record.sl }}{{ record.sfxm?.dw }} </template>
          <template v-if="column.dataIndex === 'pl'">
            {{ record.pl?.pl }}
          </template>
          <template v-if="column.dataIndex === 'orderType'">
            {{ record.orderType === 2 ? '套餐' : '单项' }}
          </template>
        </template>
      </BaseTable>
    </div>
    <template #footer>
      <div class="flex justify-between">
        <a-form class="flex items-center ml-4">
          <a-form-item label="目标处方号"><a-input v-model:value="newCfbh" placeholder="请输入" /> </a-form-item>
        </a-form>
        <div>
          <a-button @click="onCancel">取消</a-button>
          <a-button :disabled="selectedRowKeys.length === 0" type="primary" @click="onOk">确认</a-button>
        </div>
      </div>
    </template>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import BaseTable from '@/components/BaseTable'

const emit = defineEmits(['update:open', 'success'])
const props = defineProps({
  open: {
    type: Boolean,
    default: false
  },
  dataSource: {
    type: Array,
    default: () => []
  }
})

const selectedRowKeys = ref([])
const selectedRows = ref([])
const newCfbh = ref(1)
const onSelectChange = (keys, rows) => {
  selectedRowKeys.value = keys
  selectedRows.value = rows
}

const onCancel = () => {
  emit('update:open', false)
}
const tableData = ref([])

watch(
  () => props.open,
  (val) => {
    if (val) {
      tableData.value = props.dataSource.filter((i) => i.id && [2, 4].includes(i.xmlb))
      selectedRowKeys.value = tableData.value.map((item) => item.id)
      const cfbhArr = props.dataSource.map((item) => item.cfbh || 0)
      const max = cfbhArr.reduce((a, b) => Math.max(Number(a), Number(b)))
      newCfbh.value = max + 1
    }
  }
)

const onOk = async () => {
  const { code, msg } = await post('/outp/mzys/bulkPrescriptions', {
    cfbh: newCfbh.value,
    ids: selectedRowKeys.value
  })
  if (code === 0) {
    message.success(msg || '批量处方成功')
    emit('success')
    emit('update:open', false)
  } else {
    message.error(msg || '批量处方失败')
  }
}
</script>
<style lang="less" scoped>
:deep(.ant-table-row-expand-icon) {
  display: none;
}
</style>
